package com.gome.security.util.MD4;

import java.security.MessageDigest;  
import java.security.Security;  
import org.bouncycastle.jce.provider.BouncyCastleProvider;  
import org.bouncycastle.util.encoders.Hex;  
/** 
 * bouncy castle扩展支持的MD4的算法实现 
 * @author kongqz 
 * */  
public class MD4Coder {  
      
      
    /** 
     * MD4的消息摘要算法实现 
     * @param data 要做消息摘要的数据 
     * @return byte[] 消息摘要 
     *  
     * */  
    public static byte[] encodeMD4(byte[] data) throws Exception{  
          
        Security.addProvider(new BouncyCastleProvider());  
        //初始化MessageDigest  
        MessageDigest md=MessageDigest.getInstance("MD4");  
          
        return md.digest(data);  
    }  
      
    /** 
     * MD4的消息摘要算法实现，转成16进制 
     * @param data 要做消息摘要的数据 
     * @return String 消息摘要 
     *  
     * */  
    public static String encodeMD4Hex(byte[] data) throws Exception{  
          
        byte[] b= encodeMD4(data);  
          
        return new String(Hex.encode(b));  
    }  
    /** 
     * @param args 
     * @throws Exception  
     */  
    public static void main(String[] args) throws Exception {  
        String str="bouncycast 的消息摘要算法";  
        System.out.println("原文："+str);  
        byte[] data1=MD4Coder.encodeMD4(str.getBytes());  
        System.out.println("MD4的消息摘要算法值："+data1.toString());  
          
        String data2=MD4Coder.encodeMD4Hex(str.getBytes());  
        System.out.println("MD4做十六进制编码处理的消息摘要算法值："+data2);  
          
          
    }  
}  